﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace Encryption.Base
{
    class CommonUtilityBase
    {
        [DllImport("kernel32.dll")]
        private extern static IntPtr LoadLibrary(String path);//path 就是dll路径 返回结果为0表示失败。
        [DllImport("kernel32.dll")]
        private extern static IntPtr GetProcAddress(IntPtr lib, String funcName);//lib是LoadLibrary返回的句柄，funcName 是函数名称 返回结果为0标识失败。
        [DllImport("kernel32.dll")]
        private extern static bool FreeLibrary(IntPtr lib);
        [DllImport("kernel32.dll")]
        private static extern uint GetLastError();

        private IntPtr m_publicDll;

        public CommonUtilityBase()
        {
            string dllPath = @"lib\\CommonUtility.dll";
            m_publicDll = LoadLibrary(dllPath);
        }

        public Delegate GetFunction(string funcName,Type T)
        {
            IntPtr api = GetProcAddress(m_publicDll, funcName);
            return Marshal.GetDelegateForFunctionPointer(api, T);
        }

        public IntPtr GetPublicDll()
        {
            return m_publicDll;
        }

        ~CommonUtilityBase()
        {
            FreeLibrary(m_publicDll);
        }
    }
}
